//
//  MOWorkSelfAssessmentViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/15.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOWorkSelfAssessmentViewController.h"

@interface MOWorkSelfAssessmentViewController ()<UITableViewDelegate,UITableViewDataSource,XLsn0wPickerTimerDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)UIView *footerView;

@property(nonatomic,strong)UIView *bottomView;

@property(nonatomic,strong)NSMutableArray *buttonArray;
//自评内容
@property(nonatomic,strong)UITextView *contentTextView;
//日期
@property(nonatomic,strong)NSString *dateStr;
//季度
@property(nonatomic,strong)NSString *quarterStr;
//自评等级
@property(nonatomic,strong)NSString *ziPingDengJi;

@end

@implementation MOWorkSelfAssessmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self initData];
    
    [self getData];
}

-(void)creatView
{
    self.title = @"工作自评";
    
    self.view.backgroundColor = KWhiteColor;
    
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = self.footerView;
    
    [self.view addSubview:self.bottomView];
}

-(void)initData
{
    //年限
    self.dateStr = [NSString getCurrentTimesAndFormat:@"YYYY"];
    
    //季度 显示上个季度的
    NSString *jidu = @"";
    if ([[NSString getJiDu] integerValue]==1) {
        jidu = @"Q04";
        self.dateStr = [NSString stringWithFormat:@"%ld",(long)[self.dateStr integerValue]-1];
    }else if ([[NSString getJiDu] integerValue]==2){
        jidu = @"Q01";
    }else if ([[NSString getJiDu] integerValue]==3){
        jidu = @"Q02";
    }else{
        jidu = @"Q03";
    }
    self.quarterStr = jidu;
    
    self.ziPingDengJi = @"01";
    
    [self.tableView reloadData];
}

-(void)getData
{
    NSString *url = [NSString stringWithFormat:@"%@?nd=%@&jd=%@&authorization=%@",SZRSGZZP_QUERYGZZP_URL,self.dateStr,self.quarterStr,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{} success:^(id responseObject) {
        NSLog(@"工作自评查询：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            
            if (![responseObject[@"data"][0] isKindOfClass:[NSDictionary class]]) {
                self.contentTextView.text = @"";
                self.contentTextView.placeholder = @"请输入自评内容...";
                return ;
            }
            NSDictionary *dataDic = responseObject[@"data"][0];
            NSString *ziping = [NSString passNullForString:dataDic[@"gzjs"]];
            self.contentTextView.text = ziping;
            if (ziping.length>0) {
                self.contentTextView.placeholder = @"";
            }else{
                self.contentTextView.placeholder = @"请输入自评内容...";
            }
            
            self.ziPingDengJi = [NSString passNullForString:dataDic[@"zwpj"]];
            
            for (int i = 0; i<self.buttonArray.count; i++) {
                UIButton *button = self.buttonArray[i];
                if([self.ziPingDengJi isEqualToString:@"01"]){
                    if (i==0) {
                        [button setImage:IMAGE(@"work_selected") forState:UIControlStateNormal];
                    }else{
                        [button setImage:IMAGE(@"work_normal") forState:UIControlStateNormal];
                    }
                }else if ([self.ziPingDengJi isEqualToString:@"02"]){
                    if (i==1) {
                        [button setImage:IMAGE(@"work_selected") forState:UIControlStateNormal];
                    }else{
                        [button setImage:IMAGE(@"work_normal") forState:UIControlStateNormal];
                    }
                }else if ([self.ziPingDengJi isEqualToString:@"03"]){
                    if (i==2) {
                        [button setImage:IMAGE(@"work_selected") forState:UIControlStateNormal];
                    }else{
                        [button setImage:IMAGE(@"work_normal") forState:UIControlStateNormal];
                    }
                }else{
                    if (i==3) {
                        [button setImage:IMAGE(@"work_selected") forState:UIControlStateNormal];
                    }else{
                        [button setImage:IMAGE(@"work_normal") forState:UIControlStateNormal];
                    }
                }
            }
        }
    } failure:^(NSError *error) {
        NSLog(@"工作自评查询：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

#pragma mark -- 提交
-(void)clickSubmitButton
{
    if (self.contentTextView.text.length==0) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"请您输入自评内容" showInView:self.view];
        return;
    }
    
    MOHintView *hintView = [[MOHintView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) andContentStr:@"确定保存？"];
    __weak typeof (self)weakSelf = self;
    hintView.clickSureButtonBlock = ^{
        [weakSelf seaveData];
    };
    [WINDOW addSubview:hintView];
    
    
}

-(void)seaveData
{
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",SZRSGZZP_GZZPSAVE_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSDictionary *dic = @{
                          @"nd":self.dateStr,
                          @"jd":self.quarterStr,
                          @"zwpj":self.ziPingDengJi,
                          @"gzjs":self.contentTextView.text
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{@"data":[NSString dictionaryToJson:dic]} success:^(id responseObject) {
        NSLog(@"工作自评保存：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:responseObject[@"msg"] showInView:self.view];
    } failure:^(NSError *error) {
        NSLog(@"工作自评保存：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    [PPNetworkHelper GET:url parameters:dic success:^(id responseObject) {
//        NSLog(@"工作自评保存：%@",responseObject);
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//
//    } failure:^(NSError *error) {
//
//        NSLog(@"工作自评保存：%@",[error localizedDescription]);
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
//    }];
}

#pragma mark -- 选择等级
-(void)clickGradeButton:(UIButton *)sender
{
    for (int i = 0; i<self.buttonArray.count; i++) {
        UIButton *button = self.buttonArray[i];
        if (i==sender.tag) {
            [button setImage:IMAGE(@"work_selected") forState:UIControlStateNormal];
        }else{
            [button setImage:IMAGE(@"work_normal") forState:UIControlStateNormal];
        }
        [button SG_imagePositionStyle:(SGImagePositionStyleDefault) spacing:10];
    }
    switch (sender.tag) {
        case 0:
            self.ziPingDengJi = @"01";
            break;
        case 1:
            self.ziPingDengJi = @"02";
            break;
        case 2:
            self.ziPingDengJi = @"03";
            break;
        case 3:
            self.ziPingDengJi = @"04";
            break;
            
        default:
            break;
    }
}



- (void)pickerTimer:(XLsn0wPickerTimer *)pickerTimer year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    
    NSString *currentDate = [NSString getCurrentTimesAndFormat:@"YYYY"];
    if (year>[currentDate integerValue]) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"选择的时间不能大于当前时间" showInView:self.view];
        return;
    }
    self.dateStr = [NSString stringWithFormat:@"%ld", (long)year];
    
    UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    cell.detailTextLabel.text = self.dateStr;
    
    [self getData];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = KBlackColor_222222;
        cell.textLabel.font = FONT_SIZE(15);
        cell.detailTextLabel.font = FONT_SIZE(15);
        NSArray *arr = @[@"时间:",@"季度:"];
        cell.textLabel.text = arr[indexPath.row];
        
        if (indexPath.row==0) {
            if ([self.dateStr integerValue]>0) {
                cell.detailTextLabel.text = self.dateStr;
            }else{
                cell.detailTextLabel.text = @"请选择";
            }
        }else{
            if (self.quarterStr.length>0) {
                NSString *jidu = @"";
                if ([self.quarterStr isEqualToString:@"Q01"]) {
                    jidu = @"第一季度";
                }else if([self.quarterStr isEqualToString:@"Q02"]){
                    jidu = @"第二季度";
                }else if([self.quarterStr isEqualToString:@"Q03"]){
                    jidu = @"第三季度";
                }else{
                    jidu = @"第四季度";
                }
                cell.detailTextLabel.text = jidu;
            }else{
                cell.detailTextLabel.text = @"请选择";
            }
        }
        
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 13;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        
        XLsn0wPickerTimer *pickerDate = [[XLsn0wPickerTimer alloc] init];
        pickerDate.maxComponentCount = 1;
        pickerDate.xlsn0wDelegate = self;
        [pickerDate show];
        
    }else{
        
        SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSArray *tArr = @[@"第一季度",@"第二季度",@"第三季度",@"第四季度"];
        NSArray *vArr = @[@"Q01",@"Q02",@"Q03",@"Q04"];
        pickview.numberArray = tArr;
        __weak typeof (self)weakSelf = self;
        pickview.clickIndexBlock = ^(NSInteger index) {
            
            weakSelf.quarterStr = vArr[index];
            
            UITableViewCell *cell = (UITableViewCell *)[weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
            cell.detailTextLabel.text = tArr[index];
            
            [weakSelf getData];
            
        };
        [WINDOW addSubview:pickview];
    }
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight - 57 - TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}

-(UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        _footerView.backgroundColor = KWhiteColor;
        
        UILabel *gradeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 30, kHConversions(54))];
        gradeLabel.textColor = KBlackColor_222222;
        gradeLabel.font = FONT_SIZE(15);
        gradeLabel.text = @"自评等级:";
        [_footerView addSubview:gradeLabel];
        
        self.buttonArray = [NSMutableArray array];
        CGFloat buttonW = (SCREEN_WIDTH - 50)/4;
        NSArray *arr = @[@"优",@"良",@"中",@"差"];
        for (int i = 0; i<arr.count; i++) {
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20 + buttonW *i, gradeLabel.bottom, buttonW, 16)];
            [button setTitleColor:KBlackColor_222222 forState:UIControlStateNormal];
            button.titleLabel.font = FONT_SIZE(15);
            [button setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
            [button setTitle:arr[i] forState:UIControlStateNormal];
            if (i==0) {
                [button setImage:IMAGE(@"work_selected") forState:UIControlStateNormal];
            }else{
                [button setImage:IMAGE(@"work_normal") forState:UIControlStateNormal];
            }
            [button SG_imagePositionStyle:(SGImagePositionStyleDefault) spacing:10];
            button.tag = i;
            [button addTarget:self action:@selector(clickGradeButton:) forControlEvents:UIControlEventTouchUpInside];
            [_footerView addSubview:button];
            [self.buttonArray addObject:button];
        }
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, gradeLabel.bottom + 16 +kHConversions(20), SCREEN_WIDTH, 10)];
        lineView.backgroundColor = KGrayColor_ededed;
        [_footerView addSubview:lineView];
        
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, lineView.bottom, SCREEN_WIDTH - 30, kHConversions(35))];
        contentLabel.textColor = KBlackColor_222222;
        contentLabel.font = FONT_SIZE(15);
        contentLabel.text = @"自评内容:";
        [_footerView addSubview:contentLabel];
        
        self.contentTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, contentLabel.bottom, SCREEN_WIDTH - 20, 180)];
        self.contentTextView.textColor = KBlackColor_222222;
        self.contentTextView.font = FONT_SIZE(12);
        self.contentTextView.placeholder = @"请输入自评内容...";
        [_footerView addSubview:self.contentTextView];
        
        _footerView.height = self.contentTextView.bottom;
    }
    return _footerView;
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin - 57, SCREEN_WIDTH, 57)];
        
        UIButton *submitButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, _bottomView.width, _bottomView.height)];
        [submitButton setTitle:@"提交" forState:UIControlStateNormal];
        [submitButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
        submitButton.titleLabel.font = FONT_SIZE(15);
        [submitButton addTarget:self action:@selector(clickSubmitButton) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:submitButton];
    }
    return _bottomView;
}

@end
